"""
Command-line tools for histore package
"""

import sys, datetime
import easygui



def commandline_utf8():
    try:
        from ctypes          import cdll
        from ctypes.wintypes import LPCWSTR

        GetCommandLineW = cdll.kernel32.GetCommandLineW
        #GetCommandLineW.argtypes = []
        GetCommandLineW.restype  = LPCWSTR
        return GetCommandLineW()

    except Exception:
        pass



def add_link( args ):
    
    url, title = args.split(' ',1)
    title = title.replace('\\','"')
    tags  = 'link '
    
    names  = ['tags','title','url']
    values = [ tags, title, url ]    
    result = easygui.multenterbox( '', 'Add link to Histore', names, values )
    if not result:
        return

    tags, title, url = result
    tags = tags.split()

    print tags; print title; print url

    import histore.db
    histore.db.addRecord( title= title, 
                          value= url, 
                          time=  datetime.datetime.now(),
                         *tags )
        

def main():
    
    command = commandline_utf8()
    print 'command:', command
    action, args = command.split(' ',4)[3:]     # drop 'python' and '-i'

    print 'action:', `action`
    print 'args:', `args`

    if 'add_link' == action:
        add_link( args )



if '__main__' == __name__:
    main()



